<template>
	<view class="content">
		<view v-if="isWx">
			<view class="content-info" v-if="codeInfo.codekey">
				<view @click="copyDhm">
					<text class="padding-6">点击复制：<text class="codekey-text">{{ codeInfo.codekey }}</text></text>
				</view>
				<text class="padding-6">有效期：{{ codeInfo.exp_time }}</text>
				<text class="padding-6">注：一天内只能获取 {{ codeInfo.limit }} 次，请复制好兑换码后再退出</text>
			</view>
		</view>
		<view v-else>请用手机微信打开获取ε=ε=ε=(~￣▽￣)~</view>
		<view v-show="top_public && isWx && public_switch">
			<rich-text :nodes="top_public"></rich-text>
			<view class="content-form">
				<view v-if="pagesPath == ''">
					<u--input @input="changeOpenid" :value="openid" placeholder="请填写您的小程序id" border="surround"></u--input>
					<u-button @click="getFrom" type="primary" text="获取推广链接"></u-button>
				</view>
				<view v-else>
					<u-tag plain plainFill type="success" :text="pagesPath"></u-tag>
					<u-button @click="copyLink" type="primary" text="复制链接"></u-button>
					<u-button @click="resetLink" text="重新生成"></u-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			plusInfo: null,
			isWx: false,
			options: {
				from: '',
				group_id: ''
			},
			codeInfo: {},
			top_public: '',
			public_switch: 0,
			openid: '',
			pagesPath: ''
		};
	},
	onLoad(options) {
		this.plusInfo = uni.getSystemInfoSync();
		if (options.hasOwnProperty('from')) {
			this.$set(this.options, 'from', options.from);
		}
		if (options.hasOwnProperty('_g')) {
			this.$set(this.options, 'group_id', options._g);
		}
		
		let openid = uni.getStorageSync('openid');
		if (openid) {
			this.pagesPath = '/pages/index/index?from=' + openid;
			
			if (options.from == openid) {
				this.$set(this.options, 'from', '');
			}
		}
	},
	onReady() {
		this.isWx = this.checkWx();
		if (this.isWx) {
			this.gcode();
		}
	},
	methods: {
		copyDhm () {
			let txt = this.codeInfo.codekey;
			if (txt == 'null' || txt == 'undefined' || txt == '') {
				uni.showToast({
					title: '复制失败，请重新打开页面',
					icon: 'none'
				})
				return;
			}
			if (document.queryCommandSupported('copy')) {
				let textarea = document.createElement('textarea');
				textarea.value = txt;
				textarea.readOnly = 'readOnly';
				document.body.appendChild(textarea);
				textarea.select();
				textarea.setSelectionRange(0, txt.length);
				let result = document.execCommand('copy');
				textarea.remove();
				uni.showToast({
					title: '复制成功',
					icon: 'none'
				})
			} else {
				uni.showToast({
					title: '您的浏览器不支持复制',
					icon: 'none'
				})
			}
		},
		copyLink() {
			let txt = this.$config.webBaseUrl + '#' + this.pagesPath;
			if (txt == 'null' || txt == 'undefined' || txt == '') {
				uni.showToast({
					title: '复制失败，请重新生成链接',
					icon: 'none'
				})
				return;
			}
			if (document.queryCommandSupported('copy')) {
				let textarea = document.createElement('textarea');
				textarea.value = txt;
				textarea.readOnly = 'readOnly';
				document.body.appendChild(textarea);
				textarea.select(); // 选中文本内容
				textarea.setSelectionRange(0, txt.length);
				let result = document.execCommand('copy');
				textarea.remove();
				uni.showToast({
					title: '复制成功',
					icon: 'none'
				})
			} else {
				uni.showToast({
					title: '您的浏览器不支持复制',
					icon: 'none'
				})
			}
		},
		resetLink() {
			this.openid = '';
			this.pagesPath = '';
			uni.removeStorageSync('openid');
			uni.reLaunch({
				url: '/pages/index/index'
			});
		},
		changeOpenid(value) {
			this.openid = value;
			uni.setStorageSync('openid', value);
		},
		getFrom() {
			uni.reLaunch({
				url: '/pages/index/index?from=' + this.openid
			});
		},
		gcode() {
			let _this = this;
            uni.showLoading({
                title: '请求中',
            })
			_this.$http
				.post('/codekey/gcode/one', {
					device_id: _this.plusInfo.deviceId,
					model: _this.plusInfo.model,
					from: _this.options.from,
					group_id: _this.options.group_id
				})
				.then(res => {
                    uni.hideLoading();
					_this.top_public = res.data.public;
					_this.public_switch = res.data.public_switch;
					if (res.code == 200) {
						_this.codeInfo = res.data;
					}
				});
		},
		checkWx() {
			let ua = navigator.userAgent.toLowerCase();
			if (ua.match(/MicroMessenger/i) == 'micromessenger') {
				return true;
			} else {
				return false;
			}
		}
	}
};
</script>

<style scoped>
.content {
	padding: 10px 15px;
	font-size: 16px;
}
.content-info {
	display: flex;
	flex-direction: column;
	justify-content: flex-start;
	margin-bottom: 10px;
}
.padding-6 {
	padding: 6px 0;
}
.codekey-text {
	font-size: 18px;
	font-weight: bold;
}
.title {
	text-align: center;
}
.content-form {
	padding: 10px 0;
}
.content-form button {
	margin-top: 10px;
	width: 80%;
}
</style>
